package com.example.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.bean.PageRes;
import com.example.demo.bean.sysnotice.SysNoticeDO;
import com.example.demo.exception.OtherException;
import com.example.demo.mapper.sysnotice.SysNoticeMapper;
import com.example.demo.service.ISysNoticeService;
import com.example.demo.dto.sysnotice.SysNoticePageReqVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNoticeDO> implements ISysNoticeService {

    @Resource
    private SysNoticeMapper sysNoticeMapper;

    @Override
    public PageRes<SysNoticeDO> list(SysNoticePageReqVO sysNoticePageReqVO) {
        return sysNoticeMapper.selectPage(sysNoticePageReqVO);
    }

    @Override
    public int addSysNotice(SysNoticeDO sysNoticeDO) {
        return sysNoticeMapper.insert(sysNoticeDO);
    }

    @Override
    public int deleteSysNotice(SysNoticeDO sysNoticeDO) {
        validSysNoticeExist(sysNoticeDO.getNoticeId());
        return sysNoticeMapper.deleteById(sysNoticeDO);
    }

    @Override
    public int updateSysNotice(SysNoticeDO sysNoticeDO) {
        validSysNoticeExist(sysNoticeDO.getNoticeId());
        return sysNoticeMapper.updateById(sysNoticeDO);
    }

    @Override
    public SysNoticeDO getSysNotice(SysNoticeDO sysNoticeDO) {
        validSysNoticeExist(sysNoticeDO.getNoticeId());
        return sysNoticeMapper.selectById(sysNoticeDO.getNoticeId());
    }

    private void validSysNoticeExist(Long noticeId){
        SysNoticeDO exist = sysNoticeMapper.selectById(noticeId);
        if(exist == null){
            throw new OtherException("通知消息不存在");
        }
    }
}
